package com.biz.primus.ms.base.jpa.converter;

import com.biz.primus.common.utils.JsonUtils;
import javax.persistence.AttributeConverter;
import java.io.Serializable;

/**
 * Created by defei on 7/10/17.
 */
public abstract class AbstractJsonConverter<T> implements AttributeConverter<T, String>, Serializable {

    public abstract Class<T> getTargetClass();

    @Override
    public String convertToDatabaseColumn(T attribute) {

        return JsonUtils.obj2Json(attribute);
    }

    @Override
    public T convertToEntityAttribute(String dbData) {

        return JsonUtils.json2Obj(dbData, getTargetClass());
    }
}
